home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / tvcopy.exe / SLIST.PAS < prev   
Pascal/Delphi Source File  |  1993-04-18  |  12KB  |  367 lines

  1. {$X+}
  2. unit SList;                    {adapted from PhoneLst unit for Phone.pas}
  3.  
  4. interface
  5.  
  6. uses
  7.   Objects,Drivers,Views,Dialogs,App, {Turbo Vision units                 }
  8.   Printer;                           {Turbo Pascal standard unit         }
  9.  
  10. { This unit implements a TSiteColl, which is a collection of TSite
  11.   objects. It has methods which allow one to view the collection via a list
  12.   box, and to add, edit or delete records. A TSite is simply a record
  13.   containing a Site name, Latitude and Longitude, along with appropriate
  14.   Load and Store methods. TSite can easily be modified to accomodate
  15.   additional data.      }
  16.  
  17. const
  18.   cmPrintF      = 213;              { Print all TSites                    }
  19.   cmAdd         = 214;              { Add a new TSite                     }
  20.   cmEdit        = 215;              { Edit the current TSite              }
  21.   cmRemove      = 216;              { Remove the current TSite            }
  22.  
  23. type
  24.   String80 = String[80];
  25.   String60 = String[60];
  26.   String40 = String[40];
  27.   String14 = String[14];
  28.   PWord    = ^word;
  29.  
  30.   NameStr   = String40;   {was String[40] in Phone example}
  31.   NumberStr = String14;   {was String[12]}
  32.   InfoStr   = String80;
  33.  
  34.   PSite = ^TSite;
  35.   TSite = object (TObject)
  36.     Name:      NameStr;
  37.     Latitude:  NumberStr;
  38.     Longitude: NumberStr;
  39.     Info:      InfoStr;
  40.     constructor Init (AName: NameStr;
  41.                       ALatitude, ALongitude: NumberStr;
  42.                       Ainfo: InfoStr);
  43.     end;
  44.  
  45. { TSiteListBox is a simple descendant of TListBox, with a specialized
  46.   GetText method to display the TSite objects in the list box.          }
  47.  
  48.   PSiteListBox = ^TSiteListBox;
  49.   TSiteListBox = object (TListBox)
  50.     function GetText (Item: Integer; MaxLen: Integer): String; virtual;
  51.     end;
  52.  
  53. { TSiteColl is implemented as a descendant of TCollection. The Show method
  54.   opens up a dialog box which allows viewing and editing of the TSites.    }
  55.  
  56.   PSiteColl = ^TSiteColl;
  57.   TSiteColl = object (TSortedCollection)
  58.     function Compare(Key1, Key2: Pointer): Integer; virtual;
  59.     procedure FreeItem (Item: pointer); virtual;
  60.     function Show: Word;
  61.     end;
  62.  
  63. { TViewDialog is a descendant of TDialog which is used to display the TSite
  64.   information and allow for editing. SiteColl points to the associated
  65.   TSiteColl object, and L points to the list box that displays the TSites. }
  66.  
  67.   PViewDialog = ^TViewDialog;
  68.   TViewDialog = object (TDialog)
  69.     L: PSiteListBox;
  70.     SiteColl: PSiteColl;
  71.     constructor Init (ASiteColl: PSiteColl);
  72.     procedure HandleEvent (var Event: TEvent); virtual;
  73.     end;
  74.  
  75. var
  76.     ViewDialog: PViewDialog;
  77.     SiteList: PSiteColl;
  78.  
  79. { The RegisterSite procedure takes care of registering the newly defined
  80.   object types so that they can be written to or read from a stream. Only
  81.   those object types which are actually expected to be stored are
  82.   registered.                                                            }
  83.  
  84. procedure RegisterSite;
  85.  
  86. implementation   {=================================================}
  87.  
  88. { TSite methods }
  89.  
  90. constructor TSite.Init (AName: NameStr;
  91.                         ALatitude, ALongitude: NumberStr;
  92.                         AInfo: InfoStr);
  93. begin
  94.   Name :=      AName;
  95.   Latitude :=  ALatitude;
  96.   Longitude := ALongitude;
  97.   Info:=       AInfo;
  98. end;
  99.  
  100. { TSiteListBox methods }
  101.  
  102. { TSiteListBox.GetText returns a composite string containing the name,
  103.   Latitude and Longitude fields of the appropriate TSite object. It shoves
  104.   all of the fields into one string which is then displayed.             }
  105.  
  106. function TSiteListBox.GetText (Item: Integer; MaxLen: Integer): String;
  107. var
  108.   S: String;
  109. begin
  110.     {0         1         2         3         4         5         6        }
  111.     {012345678901234567890123456789012345678901234567890123456789012345678}
  112. S:=  '                                                                    ';
  113.      {  Site Name                                 Latitude     Longitude  }
  114.  
  115. Move (PSite (List^.At (Item))^.Name[1],S[1],
  116.   Length (PSite (List^.At (Item))^.Name));
  117.  
  118. Move (PSite (List^.At (Item))^.Latitude[1],S[43],
  119.   Length (PSite (List^.At (Item))^.Latitude));
  120.  
  121. Move (PSite (List^.At (Item))^.Longitude[1],S[56],
  122.   Length (PSite (List^.At (Item))^.Longitude));
  123. GetText := S;
  124. end;
  125.  
  126. { TSiteColl methods }
  127.  
  128. function StrUpCase ( S : string ) : string ;   {from JJ Stein}
  129. var
  130.    b                         : byte ;
  131. begin
  132.    for b := 1 to length ( S ) do
  133.       S [ b ]                := UpCase ( S [ b ] ) ;
  134.    StrUpCase                 := S ;
  135. end ;
  136.  
  137.   {Phonelst unit originally used a TCollection; I (SM) converted this
  138.    to a TSortedCollection with the help of ideas from Jonathan J. Stein}
  139.  
  140. function TSiteColl.Compare ;  
  141. begin
  142.         if PSite (Key1)^.Name < PSite (Key2)^.Name then Compare:= -1
  143.    else if PSite (Key1)^.Name > PSite (Key2)^.Name then Compare:=  1
  144.    else                                                   Compare:=  0;
  145. end ;
  146.  
  147.  
  148. procedure TSiteColl.FreeItem;
  149. begin
  150.   if SiteList <> nil then
  151.   begin
  152.   Dispose(PSite(Item))
  153.   end;
  154. end;
  155.  
  156.  
  157. { TSiteColl.Show ExecViews a TViewDialog, and returns
  158.   the result of that ExecView as its own result.  }
  159.  
  160. function TSiteColl.Show: Word;
  161. begin
  162.   ViewDialog := New (PViewDialog,Init (@Self));
  163.   Show := DeskTop^.ExecView (ViewDialog);
  164. end;
  165.  
  166. { ModifyRecord instantiates a dialog box which is used for adding a new
  167.   TSite record or editing an existing one. In the case of adding a record,
  168.   the calling routine passes empty strings as the values of Name, Latitude,
  169.   and Longitude in the Site parameter; upon return, Site contains the new
  170.   values of Name, Latitude & Longitude. In the case of editing, the calling
  171.   routine passes the   existing values of Name, Latitude and Longitude in
  172.   the Site parameter, and they are   replaced by the new values upon return.
  173.   ModifyRecord returns a value equal   to the result of ExecViewing the
  174.   dialog; if the dialog was cancelled by   the user, the Site parameter is
  175.   returned unaltered.                       }
  176.  
  177. function ModifyRecord ( DSite: PSite; Title: TTitleStr): Word;
  178. var
  179.   R: TRect;
  180.   D: PDialog;
  181.   N,T,G,I: PInputLine;         {Name, laT, lonG, Info}
  182.   Result: Word;
  183.  
  184. begin
  185. R.Assign (27,7,66,21);    {40 chars wide}
  186. D := New (PDialog,Init (R, Title + 'One Site'));
  187.  
  188. R.Assign (2,2,34,3);
  189. N := New (PInputLine,Init (R,40));
  190. N^.SetData (DSite^.Name);
  191. D^.Insert (N);
  192. R.Assign (2,1,31,2);
  193. D^.Insert (New (PLabel,Init (R, 'Site Name',N)));
  194.  
  195. R.Assign (2,5,16,6);
  196. T := New (PInputLine,Init (R,14));
  197. T^.SetData (DSite^.Latitude);
  198. D^.Insert (T);
  199. R.Assign (2,4,14,5);
  200. D^.Insert (New (PLabel,Init (R, 'Latitude',T)));
  201.  
  202. R.Assign (18,5,33,6);
  203. G := New (PInputLine,Init (R,14));
  204. G^.SetData (DSite^.Longitude);
  205. D^.Insert (G);
  206. R.Assign (18,4,31,5);
  207. D^.Insert (New (PLabel,Init (R, 'Longitude',G)));
  208.  
  209. R.Assign (2,8,34,9);
  210. I := New (PInputLine,Init (R,80));
  211. I^.SetData (DSite^.Info);
  212. D^.Insert (I);
  213. R.Assign (2,7,20,8);
  214. D^.Insert (New (PLabel,Init (R, 'Remarks',I)));
  215.  
  216. R.Assign (3,10,17,12);
  217. D^.Insert (New (PButton,Init (R, '~O~k',cmOK,bfDefault)));
  218. R.Assign (19,10,31,12);
  219. D^.Insert (New (PButton,Init (R, 'Cancel',cmCancel,bfNormal)));
  220. D^.SelectNext (False);
  221. Result := DeskTop^.ExecView (D);
  222. if Result <> cmCancel then
  223.   begin
  224.   N^.GetData (DSite^.Name);
  225.   T^.GetData (DSite^.Latitude);
  226.   G^.GetData (DSite^.Longitude);
  227.   I^.GetData (DSite^.Info);
  228.   end;
  229. Dispose (D,Done);
  230. ModifyRecord := Result;
  231. end;
  232.  
  233. { TViewDialog methods }
  234.  
  235. { TViewDialog.Init is a basic dialog box constructor; nothing fancy here.}
  236.  
  237. constructor TViewDialog.Init (ASiteColl: PSiteColl);
  238. var
  239.   R: TRect;
  240.   SB: PScrollBar;
  241. begin
  242. R.Assign (2,5,77,18);
  243. TDialog.Init (R, 'Site List');
  244. SiteColl := ASiteColl;
  245. R.Assign (72,2,73,7);
  246. SB := New (PScrollBar,Init (R));
  247. Insert (SB);
  248. R.Assign (2,2,72,7);
  249. L := New (PSiteListBox,Init (R,1,SB));
  250. L^.NewList (SiteColl);
  251. Insert (L);
  252. R.Assign (2,1,70,2);
  253. Insert (New (PStaticText,Init (R,
  254.   '  Site Name                                Latitude     Longitude   ')));
  255.  {0